// Ajax请求封装
import {
    baseUrl
} from './config'

function request() {}
request.get = (url = '', data, header = {
    'content-type': 'application/json'
}) => {
    return new Promise((resolve, reject) => {
        url = baseUrl + url;
        if (data) {
            url = url + '?';
            for (let prop in data) {
                url += `${prop}=${data[prop]}&`
            }
            url = url.substr(0, url.length - 1)
        }
        wx.request({
            url: url, //开发者服务器接口地址",
            method: 'GET',
            dataType: 'json', //如果设为json，会尝试对返回的数据做一次 JSON.parse
            header,
            success: res => {
                resolve(res.data.data)
            },
            fail: (err) => {
                reject(err)
            },
            complete: () => {}
        });
    })
}
request.post = (url = '', data, header = {
    'content-type': 'application/x-www-form-urlencoded'
}) => {
    return new Promise((resolve, reject) => {
        url = baseUrl + url;
        wx.request({
            url: url, //开发者服务器接口地址",
            data: data, //请求的参数",
            method: 'POST',
            dataType: 'json', //如果设为json，会尝试对返回的数据做一次 JSON.parse
            header,
            success: res => {
                resolve(res.data.data)
            },
            fail: (err) => {
                reject(err)
            },
            complete: () => {}
        });
    })
}

// 字符串解密加密
// base64加密
import {
    Base64,
    MyPass,
    _decrypt,
    _encrypt
} from './base64'

// Toast
import {
    $Toast
} from '../native/plugins/base/index';

function _showLoading(title = 'يۈكسىلىۋاتىدۇ') {
    $Toast({
        content: title,
        type: 'loading',
        mask: true
    });
}

// 格式化时间
function formatDate(value) {
    let dt = new Date(value);
    let years = dt
        .getFullYear()
        .toString()
        .padStart(2, 0);
    let month = (dt.getMonth() + 1).toString().padStart(2, 0);
    let date = dt
        .getDay()
        .toString()
        .padStart(2, 0);
    let hours = dt
        .getHours()
        .toString()
        .padStart(2, 0);
    let minutes = dt
        .getMinutes()
        .toString()
        .padStart(2, 0);
    let seconds = dt
        .getSeconds()
        .toString()
        .padStart(2, 0);
    // return `${years}-${month}-${date} ${hours}:${minutes}:${seconds}`;
    return `${years}-${month}-${date} ${hours}:${minutes}`;
}
// 格式化时间
function formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
}
/** 
 * 时间戳转化为年 月 日 时 分 秒 
 * number: 传入时间戳 
 * format：返回格式，支持自定义，但参数必须与formateArr里保持一致 
 */

function formatTime(number, format) {
    var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
    var returnArr = [];
    var date = new Date(number * 1000);
    returnArr.push(date.getFullYear());
    returnArr.push(formatNumber(date.getMonth() + 1));
    returnArr.push(formatNumber(date.getDate()));
    returnArr.push(formatNumber(date.getHours()));
    returnArr.push(formatNumber(date.getMinutes()));
    returnArr.push(formatNumber(date.getSeconds()));
    for (var i in returnArr) {
        format = format.replace(formateArr[i], returnArr[i]);
    }
    return format;
}
// sleep延迟函数
function _sleep(ms) {
    return new Promise(function (resolve, reject) {
        setTimeout(resolve, ms)
    })
}

//生成流水单子
function $orderNumber() {
    var now = new Date()
    var month = now.getMonth() + 1
    var day = now.getDate()
    var hour = now.getHours()
    var minutes = now.getMinutes()
    var seconds = now.getSeconds()
    // month = now.setMonth(month)
    month = month.toString().padStart(2, '0')
    day = day.toString().padStart(2, '0')
    // hour = now.setHours(hour)
    hour = hour.toString().padStart(2, '0')

    // minutes = now.setMinutes(minutes)
    minutes = minutes.toString().padStart(2, '0')
    // seconds = now.setSeconds(seconds)
    seconds = seconds.toString().padStart(2, '0')
    var time = now.getFullYear().toString() + month + day + hour + minutes + seconds
    return (time + (Math.floor((Math.random() * 1000000)).toString()).padStart(6, '9'))
}

function throttle(func, wait) {
    let previous = 0;
    return function () {
        console.log("okkk");
        let now = +new Date();
        let remain = wait - (now - previous);
        if (remain < 0) {
            previous = now;
            func.call(this, arguments);
        }
    };
};
export {
    throttle,
    request,
    Base64,
    MyPass,
    $Toast,
    formatDate,
    _showLoading,
    _sleep,
    _decrypt,
    _encrypt,
    formatTime,
    $orderNumber
}